package com.xuexisheqv.subject.domain.handler.subject;

import com.xuexisheqv.subject.common.enums.IsDeletedFlagEnum;
import com.xuexisheqv.subject.common.enums.SubjectInfoTypeEnum;

import com.xuexisheqv.subject.domain.convert.MultiSubjectConverter;
import com.xuexisheqv.subject.domain.entity.SubjectAnswerBO;
import com.xuexisheqv.subject.domain.entity.SubjectInfoBO;
import com.xuexisheqv.subject.domain.entity.SubjectOptionBO;

import com.xuexisheqv.subject.infra.basic.entity.SubjectMultiple;
import com.xuexisheqv.subject.infra.basic.service.SubjectMultipleService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 多选题目策略类
 */

@Component
public class MultipleTypeHandler implements SubjectTypeHandler {
    @Resource
    private SubjectMultipleService subjectMultipleService;

    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.MULTYPE;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        // 多选题目的插入
        if (subjectInfoBO == null) {
            return;
        }
        List<SubjectMultiple> subjectMultipleList = new LinkedList<>();
        subjectInfoBO.getOptionList().forEach(option -> {
            // 转换为多选题
            SubjectMultiple subjectMultiple = MultiSubjectConverter.INSTANCE.convertBoToEntity(option);
            subjectMultiple.setSubjectId(subjectInfoBO.getId());
            subjectMultiple.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
            subjectMultipleList.add(subjectMultiple);
        });
        subjectMultipleService.batchInsert(subjectMultipleList);
    }

    @Override
    public SubjectOptionBO query(int subjectId) {
        SubjectMultiple subjectMultiple = new SubjectMultiple();
        subjectMultiple.setSubjectId(Long.valueOf(subjectId));
        List<SubjectMultiple> multipleList = subjectMultipleService.queryByConditon(subjectMultiple);
        List<SubjectAnswerBO> answerBOList = MultiSubjectConverter.INSTANCE.convertEntityToBoList(multipleList);
        SubjectOptionBO subjectOptionBO = new SubjectOptionBO();
        subjectOptionBO.setOptionList(answerBOList);
        return subjectOptionBO;
    }


}
